package com.yx.cloud.network.ble;

import com.yx.cloud.network.DataPacket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Cmd {
    private static Comparator<DataPacket> comparator = new Comparator<DataPacket>() {
        @Override
        public int compare(DataPacket o1, DataPacket o2) {
            if (o1.getOrder() > o2.getOrder()) {
                return 1;
            } else if (o1.getOrder() < o2.getOrder()) {
                return -1;
            } else {
                return 0;
            }
        }
    };
    private boolean isComplete=false;
    private List<DataPacket> dataPacketList = new ArrayList<>();
    private byte cmd=-1;
    private byte[] content=null;

    public Cmd() {
    }

    public Cmd(byte cmd, byte[] content) {
        this.cmd = cmd;
        this.content = content;
    }

    public List<DataPacket> getDataPacketList() {
        return dataPacketList;
    }


    public byte getCmd() {
        return cmd;
    }

    public void setCmd(byte cmd) {
        this.cmd = cmd;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

    public boolean isComplete() {
        return isComplete;
    }

    public void addSlicePackage(DataPacket dataPacket) {

        //如果有相同order 覆盖
        int contain = -1;
        for (int i = 0; i < dataPacketList.size(); i++) {
            if (dataPacketList.get(i).getOrder() == dataPacket.getOrder()) {
                contain = i;
                break;
            }
        }
        if (contain < 0) {
            dataPacketList.add(dataPacket);
        } else {
            dataPacketList.set(contain, dataPacket);
        }

        cmd = (byte)dataPacket.getCmd();
    }

    public boolean checkComplete() {
        if (dataPacketList.size() <= 0) {
            return false;
        }

        DataPacket dataPacket = dataPacketList.get(0);
        if (dataPacketList.size() != dataPacket.getPacketSum()) {
            return false;
        }

        //排序 然后 一个个找有没有对应序号
        Collections.sort(dataPacketList, comparator);

        boolean isOrderCorrect = true;
        for (int i = 0; i < dataPacket.getPacketSum(); i++) {
            if (dataPacketList.get(i).getOrder() == i + 1) {
                continue;
            } else {
                isOrderCorrect = false;
                break;
            }
        }

        isComplete = isOrderCorrect;

        //如果包完整, 组装出数据
        if (isComplete) {
            assembleCmd();
        }

        return isOrderCorrect;
    }


    private void assembleCmd() {
        if (!isComplete) {
            return;
        }

        int cmdLength = 0;
        for (int i = 0; i < dataPacketList.size(); i++) {
            cmdLength += dataPacketList.get(i).getPacketLength() - 1;
        }

        content = new byte[cmdLength];
        int position = 0;
        for (int i = 0; i < dataPacketList.size(); i++) {
            byte[] data = dataPacketList.get(i).getContentData();
            System.arraycopy(data, 0, content, position, data.length);
            position += data.length;
        }

    }

    public void clear() {
        dataPacketList.clear();
        cmd = -1;
        isComplete = false;
        content = null;
    }

    @Override
    public String toString() {
        return "Cmd{" +
                "cmd=" + cmd +
                ", content=" + Arrays.toString(content) +
                '}';
    }
}
